<!--
 * @Date: 2020-02-24 12:08:16
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-12 16:17:22
 * @describe: -添加/修改写字楼出租-
 -->
<template>
  <el-dialog :visible.sync="visible" title="写字楼出租" @close="CloseDialog_" :destroy-on-close="true">
    <el-form
      :model="dataForm"
      ref="dataForm"
      label-width="100px"
      class="fromStyle"
       :rules="rules"
      inline
      v-loading="loading"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="楼盘" prop="dictId" class="from_item_mini">
            <el-select
              v-model="dataForm.dictId"
              filterable
              placeholder="关键字搜索"
              style="width:100%"
              :disabled="!!houseId"
            >
              <el-option
                v-for="item in dictList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                @click.native="LPlistChange(item)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="座栋" prop="zdId" class="from_item_mini">
            <el-select
              v-model="dataForm.zdId"
              @change="ZDlistChange"
              style="width:100%"
              :disabled="!!houseId"
            >
              <el-option v-for="item in zdList" :key="item.id" :label="item.num" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元" prop="dyId" class="from_item_mini">
            <el-select
              v-model="dataForm.dyId"
              @change="DYlistChange"
              style="width:100%"
              :disabled="!!houseId"
            >
              <el-option
                v-for="item in dyList"
                :key="item.id"
                :label="item.num+'单元'"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="房号" prop="num" class="from_item_mini">
            <el-input v-model="dataForm.num" placeholder="房号" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="行政区域" prop="districtCode" v-if="!houseId" class="from_item_mini">
            <el-select
              v-model="dataForm.districtCode"
              disabled
              style="width:100%"
              placeholder="行政区域"
            >
              <el-option
                v-for="item in this.$store.state.CityRegion"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商圈" prop="business" v-if="!houseId" class="from_item_mini">
            <el-select v-model="dataForm.business" style="width:100%" placeholder="商圈" disabled>
              <el-option
                v-for="item in this.$store.state.BusinessPage"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物业类型" prop="type" class="from_item_mini">
            <ren-select
              v-model="dataForm.type"
              dict-type="dictType"
              placeholder="物业类型"
              :disabled="true"
              style="width:100%"
            ></ren-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider></el-divider>

      <el-row>
        <el-col :span="2">
          <span style="line-height:28px">格局：</span>
        </el-col>
        <el-col :span="7">
          <el-form-item
            prop="apartmentB"
            class="from_item_sty1"
            :rules="[
          { required: true, message: '办公室不能为空'},
         { type: 'number', message: '办公室必须为数字值'}
          ]"
          >
            <el-input v-model.number="dataForm.apartmentB">
              <span slot="prefix">办公室 &nbsp;&nbsp;&nbsp;|</span>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item
            prop="apartmentH"
            class="from_item_sty1"
            :rules="[
      { required: true, message: '会议室不能为空'},
      { type: 'number', message: '会议室必须为数字值'}
    ]"
          >
            <el-input v-model.number="dataForm.apartmentH">
              <span slot="prefix">会议室 &nbsp;&nbsp;&nbsp;|</span>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item
            prop="apartmentG"
            class="from_item_sty1"
            :rules="[
      { required: true, message: '工位不能为空'},
      { type: 'number', message: '工位必须为数字值'}
    ]"
          >
            <el-input v-model.number="dataForm.apartmentG">
              <span slot="prefix">工位 &nbsp;&nbsp;&nbsp;|</span>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="面积" prop="area" v-if="!houseId">
        <el-input v-model="dataForm.area" placeholder="面积" @change="Setprices"></el-input>
      </el-form-item>
      <el-form-item label="装修情况" prop="renovation">
        <ren-select
          v-model="dataForm.renovation"
          dict-type="dictRenovation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="单价" prop="price"  class="from_item_small">
        <el-input v-model="dataForm.price" placeholder="单价" @change="Setprices">
          <span slot="suffix" class="imgNull">元/㎡</span>
        </el-input>
      </el-form-item>
      <el-form-item label="税收" prop="taxation">
        <ren-select
          v-model="dataForm.taxation"
          dict-type="houseTaxation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="总价" prop="prices" >
        <el-input v-model="dataForm.prices" placeholder="总价">
          <span slot="suffix" class="imgNull">元/月</span>
        </el-input>
      </el-form-item>
      <el-form-item label="付款方式" prop="tenanPay">
        <ren-select
          v-model="dataForm.tenanPay"
          dict-type="dictTenanPay"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="看房方式" prop="lookMode">
        <ren-select
          v-model="dataForm.lookMode"
          dict-type="dictLookMode"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="合同年限" prop="tenanTime">
        <el-input v-model="dataForm.tenanTime">
          <span slot="suffix" class="imgNull">年</span>
        </el-input>
      </el-form-item>
      <el-form-item label="免租天数" prop="tenanFreeTime" >
        <el-input v-model="dataForm.tenanFreeTime">
          <span slot="suffix" class="imgNull">天</span>
        </el-input>
      </el-form-item>
      <el-form-item label="递增" prop="increase">
        <el-input v-model="dataForm.increase">
          <span slot="suffix" class="imgNull">%</span>
        </el-input>
      </el-form-item>
      <el-form-item label="佣金" prop="servicePrice">
        <el-input v-model="dataForm.servicePrice">
          <span slot="suffix" class="imgNull">元</span>
        </el-input>
      </el-form-item>
      <el-form-item label="朝向" prop="direction">
         <ren-select
          v-model="dataForm.direction"
          dict-type="dictDirection"
          placeholder="房屋朝向"
          class="w100"
        ></ren-select>
      </el-form-item>
      <el-form-item label="通上下水" prop="water">
        <ren-select
          v-model="dataForm.water"
          dict-type="houseWater"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-divider></el-divider>
      <el-form-item label="家具情况" prop="furniture">
        <el-select v-model="dataForm.furniture" class="FormSelect" style="width:100%">
          <el-option label="没有" :value="0"></el-option>
          <el-option label="有" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="空调情况" prop="heating">
        <ren-select
          v-model="dataForm.heating"
          dict-type="houseHeating"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="能否注册" prop="register">
        <el-select v-model="dataForm.register" class="FormSelect" style="width:100%">
          <el-option label="不能" :value="0"></el-option>
          <el-option label="能" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入住时间" prop="inTime">
        <ren-select
          v-model="dataForm.inTime"
          dict-type="houseInTime"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源状态" prop="renovation" v-if="!houseId">
        <ren-select
          v-model="dataForm.state"
          dict-type="dictState"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="公私盘" prop="renovation" v-if="!houseId">
        <ren-select
          v-model="dataForm.commonState"
          dict-type="houseCommonState"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源归类" prop="sortType">
        <ren-select
          v-model="dataForm.sortType"
          dict-type="houseSortType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房屋现状" prop="situation" v-if="!houseId">
        <ren-select
          v-model="dataForm.situation"
          dict-type="dictSituation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房子来源" prop="source">
        <ren-select
          v-model="dataForm.source"
          dict-type="houseSource"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源类型" prop="houseType">
        <ren-select
          v-model="dataForm.houseType"
          dict-type="houseType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="姓名" prop="ownerName" v-if="!houseId">
        <el-input v-model="dataForm.ownerName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话1" prop="ownerPhone1" v-if="!houseId">
        <el-input v-model="dataForm.ownerPhone1" placeholder="电话1"></el-input>
      </el-form-item>
      <el-form-item label="电话2" prop="ownerPhone2" v-if="!houseId">
        <el-input v-model="dataForm.ownerPhone2" placeholder="电话2"></el-input>
      </el-form-item>
      <el-form-item label="电话3" prop="ownerPhone3" v-if="!houseId">
        <el-input v-model="dataForm.ownerPhone3" placeholder="电话3"></el-input>
      </el-form-item>
      <el-form-item label="标题" prop="title" class="FormitemW100">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="配置" prop="conf" class="FormitemW100">
        <el-checkbox-group v-model="confList" @change="confListChange">
          <el-checkbox
            :label="item.dictValue"
            v-for="(item,index) in $getDictDataList('dictConf')"
            :key="index"
          >{{item.dictLabel}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="标签" prop="houseLabel" class="FormitemW100">
        <el-checkbox-group v-model="LabelList" @change="LabelListChange">
          <el-checkbox
            :label="item.dictValue"
            v-for="(item,index) in $getDictDataList('houseLabel')"
            :key="index"
          >{{item.dictLabel}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="备注" prop="remake" class="FormitemW100">
        <el-input v-model="dataForm.remark" placeholder="备注" type="textarea" :rows="2"></el-input>
      </el-form-item>

      <!--   <el-form-item label="高低楼层" prop="situation">
        <ren-select
          v-model="dataForm.height"
          dict-type="dictStoreyHeight"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="电梯" prop="lift">
        <el-select v-model="dataForm.lift" class="FormSelect" style="width:100%">
          <el-option label="没有" value="0"></el-option>
          <el-option label="有" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="梯户比" prop="liftRoom">
        <el-input v-model="dataForm.liftRoom" placeholder="免租月数"></el-input>
      </el-form-item>
      <el-form-item label="租赁方式" prop="leaseType">
        <ren-select
          v-model="dataForm.leaseType"
          dict-type="houseLeaseType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="看房时间" prop="seeTime">
        <ren-select
          v-model="dataForm.seeTime"
          dict-type="houseSeeTime"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="押金" prop="deposit">
        <el-input v-model="dataForm.deposit">
          <span slot="suffix" class="imgNull">元</span>
        </el-input>
      </el-form-item>
      <el-form-item label="归属" prop="ascription">
        <ren-select
          v-model="dataForm.ascription"
          dict-type="ascription"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="权属" prop="trading">
        <ren-select
          v-model="dataForm.trading"
          dict-type="houseTrading"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="描述" prop="description" class="FormitemW100">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" v-if="!houseId" @click="dataFormSubmitHandle()">添加</el-button>
      <el-button type="primary" v-else @click="dataFormUpdata()">修改</el-button>
    </template>
    <roomlist
      :getDYFJListID="dataForm.dyId"
      :transactionType="dataForm.transactionType"
      ref="RoomListTab"
      @returnFJid="setFJid"
    ></roomlist>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import roomlist from "../../room/RoomList";
export default {
  data() {
    return {
      visible: true,
      loading: false,
      dataForm: {
        id: "",
        dictId: "",
        zdId: "",
        dyId: "",
        fjId: "",
        num: "",
        type: "",
        // businessType: "",
        transactionType: "1",
        houseType: "",
        sortType: "",
        districtCode: "",
        business: "",
        situation: "",
        ownerName: "",
        ownerPhone1: "",
        ownerPhone2: "",
        area: "",
        price: "",
        prices: "",
        key: "",
        image: "",
        renovation: "",
        furniture: "",
        lookMode: "",
        source: "",
        followTime: "",
        maintain: "",
        deptId: "",
        examine: "",
        extranet: "",
        tenantCode: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
        remark: "",
        conf: "",
        tenanFreeTime: "",
        apartmentB: "",
        apartmentH: "",
        apartmentG: "",
        servicePrice: "",
        title: "",
        tenanPay: "",
        state: "1",
        height: "",
        commonState: "1",
        lift: "",
        liftRoom: "",
        heating: "",
        water: "",
        leaseType: "",
        seeTime: "",
        inTime: "",
        increase: "",
        deposit: "",
        tenanTime: "",
        ascription: "",
        trading: "",
        description: "",
        houseLabel: "",
        taxation: "",
         direction:""
      },
      dictList: [],
      zdList: [],
      dyList: [],
      fjList: [],
      confList: [],
      LabelList: [],
       rules: {
        area: [
          {pattern: /^\d+(\.\d+)?$/, required: true,message: '面积格式只能为大于0的数切不为空', trigger: 'blur' }
        ],
         price: [
          {pattern: /^\d+(\.\d+)?$/, required: true,message: '单价格式只能为大于0的数切不为空', trigger: 'blur' }
        ],
         prices: [
          {pattern: /^\d+(\.\d+)?$/, required: true,message: '总价格式只能为大于0的数切不为空', trigger: 'blur' }
        ],
         tenanFreeTime: [
          {pattern: /^\d+$/, message: '免租天数只能为大于0的整数', trigger: 'blur' }
        ],
        tenanTime: [
          {pattern: /^\d+$/, message: '合同年限只能为大于0的整数', trigger: 'blur' }
        ],
        
         increase: [
          {pattern: /^\d+(\.\d+)?$/, message: '押金递增只能为大于0的数', trigger: 'blur' }
        ],
        ownerPhone1: [
          { required: true, message: "电话号码不能为空", trigger: "blur" },
          {pattern: /^1[345789]\d{9}$/, message: '您的手机号码输入错误', trigger: 'blur' }
        ]
      }
    };
  },
  props: ["houseId", "houseinto"],
  components: {
    roomlist
  },
  created() {
    this.getDictList();
    if (this.houseId) {
      this.dataForm.houseId = this.houseId;
      this.init();
    } else {
      this.dataForm.houseId = "";
    }
    if (this.houseinto) {
      this.dataForm.area = this.houseinto.area;
      this.dataForm.ownerName = this.houseinto.ownerName;
      this.dataForm.ownerPhone1 = this.houseinto.ownerPhone1;
      this.dataForm.ownerPhone2 = this.houseinto.ownerPhone2;
    }
  },
  methods: {
    init() {
      this.loading = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.getInfo();
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/house/houseinfo/officeLeaseInfo/`, {
          params: {
            houseId: this.dataForm.houseId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
          this.loading = false;
          if (res.data.conf) {
            this.confList = res.data.conf.split(",");
          } else {
            this.confList = [];
          }
          if (res.data.houseLabel) {
            this.LabelList = res.data.houseLabel.split(",");
          } else {
            this.LabelList = [];
          }
        })
        .catch(() => {});
    },
    //配置change
    confListChange(list) {
      this.dataForm.conf = list.join();
    },
    //标签change
    LabelListChange(list) {
      this.dataForm.houseLabel = list.join();
    },
    //获取楼盘列表
    getDictList() {
      let _this = this;
      this.$http
        .get(`/house/houseinfo/dictList`, {
          params: {
            name: ""
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dictList = res.data;
        })
        .catch(() => {});
    },
    //楼盘列表change
    LPlistChange(item) {
      this.zdList = [];
      this.dyList = [];
      this.fjList = [];
      this.dataForm.dictId = item.id;
      this.dataForm.dyId = "";
      this.getZdList(item.id);
      this.dataForm.type = item.type;
      this.dataForm.districtCode = item.districtCode;
      this.dataForm.business = item.business;
    },
    //获取楼盘座栋列表
    getZdList(id) {
      let _this = this;
      this.zdList = [];
      this.dataForm.zdId = "";
      this.$http
        .get(`/house/houseinfo/zdList`, {
          params: {
            dictId: id,
            num: ""
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.zdList = res.data;
        })
        .catch(() => {});
    },

    //座栋change
    ZDlistChange(val) {
      this.getDyList(val);
    },

    //获取楼盘座栋单元列表
    getDyList(id) {
      let _this = this;
      this.dyList = [];
      this.dataForm.dyId = "";
      this.$http
        .get(`/house/houseinfo/dyList`, {
          params: {
            zdId: id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dyList = res.data;
        })
        .catch(() => {});
    },

    //单元列表change
    DYlistChange(val) {
      this.$refs.RoomListTab.visible = true;
    },

    //获取房间信息
    getFjInfo() {
      let _this = this;
      this.$http
        .get(`/house/houseinfo/fjInfo`, {
          params: {
            id: this.dataForm.fjId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
          this.dataForm.id = null;
          if (this.houseinto) {
            this.dataForm.area = this.houseinto.area;
            this.dataForm.ownerName = this.houseinto.ownerName;
            this.dataForm.ownerPhone1 = this.houseinto.ownerPhone1;
            this.dataForm.ownerPhone2 = this.houseinto.ownerPhone2;
          }
        })
        .catch(() => {});
    },
    Setprices() {
     this.dataForm.prices = this.dataForm.area * this.dataForm.price;
    },

    //设置房间
    setFJid(item) {
      this.dataForm.fjId = item.id;
      this.dataForm.num = item.num;
      this.dataForm.storeyCoordinate = item.storeyCoordinate;
      // this.getFjInfo(item.id);
    },
    CloseDialog_() {
      this.$emit("CloseDialog");
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.dataForm.id = null;
          this.$http
            .post("/house/houseinfo/saveOfficeLeaseInfo", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  if (this.houseinto) {
                    this.$emit("deleteResources");
                  }
                  this.$emit("refreshDataList");
                  this.$refs["dataForm"].resetFields();
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
    // 修改表单提交
    dataFormUpdata: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http
            .put("/house/houseinfo/officeLease/", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message.success("修改成功！");
              this.visible = false;
              this.$refs["dataForm"].resetFields();
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  },
  watch: {
    "dataForm.transactionType": {
      handler(nowVal, oldVal) {
        if (nowVal.toString() == "1") {
         
         // this.dataForm.price = this.dataForm.leasePrice;
         // this.dataForm.prices = this.dataForm.leasePrices;
        } else if (nowVal.toString() == "0") {
          
         // this.dataForm.price = this.dataForm.sellPrice;
         // this.dataForm.prices = this.dataForm.sellPrices;
        }
      }
    }
  }
};
</script>
